package cn.doit.day05

import scala.collection.mutable.ListBuffer
import scala.io.Source

/**
*1,4
*2,5
*4,6
*2,4
*3,6
*4,6
*1,5
 */
object _03_线段重叠案例 {
  def main(args: Array[String]): Unit = {
    //读取文件 1,4   2,5
    val lines: List[String] = Source.fromFile("data/line.txt").getLines().toList
    //针对1,4怎么处理 将1，4 变成  1-2  2-3  3-4 三段
    lines.flatMap(line=>{
      val arr: Array[String] = line.split(",")
      val range: Range = arr(0).toInt until arr(1).toInt
      val list: List[Int] = range.toList
      list
    })
      .groupBy(word=>word)
      .toList
      .map(tp=>{
        val word: Int = tp._1
        val count: Int = tp._2.size
        (word+"-"+(word+1),count)
      })
      .sortBy(_._1)
      .foreach(println)

    println("========分隔符===========")

    //spark里面  多个task进行得

    val list: ListBuffer[Int] = new ListBuffer[Int]()
    lines.foreach(line=>{
      val arr: Array[String] = line.split(",")
      val start: String = arr(0)
      val end: String = arr(1)
      for(elem <- start.toInt until end.toInt){
        list.append(elem)
      }
    })
    //接下来就是哦wordcount
    list.groupBy(word=>word)
      .toList
      .map(tp=>{
        val word: Int = tp._1
        val count: Int = tp._2.size
        (word+"-"+(word+1),count)
      })
      .sortBy(_._1)
      .foreach(println)

  }

}
